package com.hzncc.flowable_diboot.controller.bpm;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.diboot.core.controller.BaseCrudRestController;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.iam.annotation.OperationCons;
import com.hzncc.flowable_diboot.dto.bpm.BpmCategoryDTO;
import com.hzncc.flowable_diboot.entity.bpm.BpmCategory;
import com.hzncc.flowable_diboot.service.bpm.BpmCategoryService;
import com.hzncc.flowable_diboot.vo.bpm.BpmCategoryDetailVO;
import com.hzncc.flowable_diboot.vo.bpm.BpmCategoryListVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 流程分类 相关rest接口
* @author MyName
* @version 1.0
* @date 2024-06-20
* Copyright © MyCorp
*/
@Slf4j
@RequestMapping("/bpm-category")
@BindPermission(name = "流程分类")
@RestController
public class BpmCategoryController extends BaseCrudRestController<BpmCategory> {

    @Autowired
    private BpmCategoryService bpmCategoryService;

    /**
    * 查询资源对象的列表VO记录
    * <p>
    * url请求参数示例: ?fieldA=abc&pageSize=20&pageIndex=1&orderBy=id
    * </p>
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping()
    public JsonResult<List<BpmCategoryListVO>> getListVOMapping(BpmCategoryDTO queryDto, Pagination pagination) throws Exception {
        return super.getViewObjectList(queryDto, pagination, BpmCategoryListVO.class);
    }

    /**
    * 根据id查询资源对象的详情VO
    * @param id ID
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult<BpmCategoryDetailVO> getDetailVOMapping(@PathVariable("id")String id) throws Exception {
        BpmCategoryDetailVO detailVO = bpmCategoryService.getViewObject(id, BpmCategoryDetailVO.class);
        return JsonResult.OK(detailVO);
    }

    /**
    * 创建资源对象数据
    * @param bpmCategory
    * @return JsonResult
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping()
    public JsonResult<?> createModelMapping(@RequestBody BpmCategory bpmCategory) throws Exception {
        LambdaQueryWrapper<BpmCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BpmCategory::getName,bpmCategory.getName()).eq(BpmCategory::isDeleted, NumberUtils.INTEGER_ZERO);
        boolean nameExists = bpmCategoryService.exists(queryWrapper);
        if (nameExists){
            return JsonResult.FAIL_EXCEPTION("分类名称已存在");
        }
        queryWrapper.clear();
        queryWrapper.eq(BpmCategory::getCode,bpmCategory.getCode()).eq(BpmCategory::isDeleted, NumberUtils.INTEGER_ZERO);
        boolean codeExists = bpmCategoryService.exists(queryWrapper);
        if (codeExists){
            return JsonResult.FAIL_EXCEPTION("分类编码已存在");
        }
        boolean success = bpmCategoryService.createEntity(bpmCategory);
        log.debug("新建数据 流程分类:{} {}", bpmCategory.getId(), success? "成功":"失败");
        return JsonResult.OK(bpmCategory.getId());
    }

    /**
    * 批量创建资源对象数据
    * @param bpmCategoryList
    * @return JsonResult
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_BATCH_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch")
    public JsonResult<?> batchCreateModelMapping(@RequestBody List<BpmCategory> bpmCategoryList) throws Exception {
        boolean success = bpmCategoryService.createEntities(bpmCategoryList);
        log.debug("批量新建数据 流程分类 {}", success? "成功":"失败");
        return JsonResult.OK(success);
    }

    /**
    * 根据id更新资源对象
    * @param bpmCategory
    * @return JsonResult
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult<?> updateModelMapping(@PathVariable("id") String id,@RequestBody BpmCategory bpmCategory) throws Exception {
        BpmCategory entityById = this.getEntity(bpmCategory.getId());
        if (ObjectUtils.isEmpty(entityById)){
            return JsonResult.FAIL_EXCEPTION("表单不存在！");
        }
        boolean success = bpmCategoryService.updateEntity(bpmCategory);
        log.debug("更新数据 流程分类:{} {}", bpmCategory.getId(), success? "成功":"失败");
        return JsonResult.OK(success);
    }

    /**
    * 根据id删除资源对象
    * @param id
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult<?> deleteModelMapping(@PathVariable("id") String id) throws Exception {
        boolean success = bpmCategoryService.deleteEntity(id);
        log.debug("删除数据 流程分类:{} {}", id, success? "成功":"失败");
        return JsonResult.OK(success);
    }

    /**
    * 根据ids集合删除资源对象
    * @param ids
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_BATCH_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch-delete")
    public JsonResult<?> deleteEntityMapping(@RequestBody List<String> ids) throws Exception {
        boolean success = bpmCategoryService.deleteEntities(ids);
        log.debug("批量删除数据 流程分类:{} {}", ids, success? "成功":"失败");
        return JsonResult.OK(success);
    }
}